---
import { keystoneContext } from '../keystone/context'

// Using Broswer Name from the user-agent request header to illustrate how to use the keystone context
// with a customised session from Astro.
// This is a contrived example of how Keystone's Access Control can work within Astro.
const userAgent = Astro.request.headers.get('user-agent')

// Match the browser name using a regular expression
const browserName = userAgent?.match(/(Chrome|Firefox)/)?.pop() ?? 'unknown'
console.log('Browser name in Astro from userAgent header', browserName)

// Create a new context with a customised session
const context = keystoneContext.withSession({
  browser: browserName.toLowerCase(),
  user: 'astro',
})
const posts = await context.query.Post.findMany({
  query: 'title id',
})
---

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
    <meta name="viewport" content="width=device-width" />
    <meta name="generator" content={Astro.generator} />
    <style>
      body {
        font: 100% system-ui;
      }
      html {
        color-scheme: light dark;
      }
    </style>
    <title>Astro</title>
  </head>
  <body>
    <h1>Astro</h1>
    <p>You browser is {browserName}</p>
    <p>Here are a list of Posts for your browser</p>
    <ul>
      {
        posts.map(post => (
          <li>
            <p>{post.title}</p>
          </li>
        ))
      }
    </ul>
  </body>
</html>
